<template>
  <el-dialog
    title="审核操作"
    :visible.sync="dialogVisible"
    :close-on-click-modal="false"
  >
    <el-form
      ref="main-form"
      :model="form"
      :rules="rules"
      label-width="80px"
      :inline="true"
    >
      <el-form-item label="订单号" size="mini" prop="tradeNo">
        <el-input
          placeholder=""
          disabled
          v-model="form.tradeNo"
          @keyup.enter.native="submit"
          :readonly="tradeNo == null"
          clearable
        ></el-input>
      </el-form-item>

      <el-form-item label="审核意见" size="mini" prop="transReason">
        <el-input
          placeholder=""
          v-model="form.transReason"
          @keyup.enter.native="submit"
          :readonly="tradeNo == null"
          clearable
        ></el-input>
      </el-form-item>
      <el-row>
        <el-form-item label=" " size="mini" prop="">
          确定{{ startType == "First" ? "初审" : "终审" }}通过吗？
        </el-form-item>
      </el-row>
      <el-row>
        <el-form-item label="  " size="mini" prop="">
          类型：{{ startOptions[auditType] }}
        </el-form-item>
      </el-row>
    </el-form>

    <div slot="footer" class="dialog-footer">
      <el-button @click="close" size="small">取 消</el-button>
      <el-button @click="submit(false)" type="danger" size="small"
        >驳回
      </el-button>
      <el-button
        type="primary"
        @click="submit(true)"
        size="small"
        :loading="loading"
        >通 过</el-button
      >
    </div>
  </el-dialog>
</template>

<script>

import ApiMain from '@/api/trans.audit';
import { checkButtonAccess } from '@/filters';

export default {
  mixins: [
  ],
  props: [
  ],
  data() {
    return {

      dialogVisible: false,
      loading: false,

      form: {
        tradeNo: '',
        transReason: '',
        transReason: '',
      },
      auditTypeText: "",
      startType: "",
      auditType: 0,
      //   1 拒付 2 冻结 3 解冻 4 退款
      startOptions: {
        1: '拒付',
        2: '冻结',
        3: '解冻',
        4: '退款'
      },
      rules: {
        tradeNo: [
          { required: true, message: '请输入单号', trigger: 'blur' },
        ],
        transReason: [
          { required: true, message: '请输入审核意见', trigger: 'blur' },
        ],
      },

      tradeNo: null,
    }
  },
  mounted() {
  },

  methods: {

    show(tradeNo = null, startType, auditType, auditTypeText) {
      this.auditType = auditType
      this.auditTypeText = auditTypeText
      this.startType = startType
      this.tradeNo = tradeNo;
      this.reset();
      this.form.tradeNo = tradeNo;
      this.dialogVisible = true;
    },

    close() {
      this.dialogVisible = false;
      this.$emit('dialog-close');
    },

    reset() {
      this.form = {
        tradeNo: '',
        transReason: '',
        transReason: '',
      };
    },



    verifyForm() {
      var bool = true;
      this.$refs['main-form'].validate((valid) => bool = valid);
      return bool;
    },

    async submit(start) {
         this.form.isApprove =start
      if (!this.verifyForm()) return false;
      if (!this.auditTypeText) return this.$message({ message: '发生错误！', type: 'success' });
      this.loading = true;

      try {
        if (this.startType == 'First') {
          await this.submitFirst();
        }
        else {
          await this.submitSecond();
        }
      } finally {
        this.loading = false;
      }
    },

    async submitFirst() {
      //  初审 
      this.form.tradeNoList = [this.form.tradeNo]
      let form = Object.assign({}, this.form);
      await ApiMain[this.auditTypeText + 'First'](form);
      this.$message({ message: '审核成功！', type: 'success' });
      this.close();

    },

    async submitSecond() {
      // 复审
      this.form.tradeNoList = [this.form.tradeNo]
      let form = Object.assign({}, this.form);
      await ApiMain[this.auditTypeText + 'Second'](form);
      this.$message({ message: '审核成功！', type: 'success' });
      this.close();
    },
  }
}
</script>